package com.eleadmin.ocean.biz.impl;

import com.eleadmin.common.system.entity.User;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.ocean.biz.OceanBulkCargoAddressBiz;
import com.eleadmin.ocean.entity.OceanBulkCargoAddress;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.ocean.result.OceanBulkCargoAddressResult;
import com.eleadmin.order.result.SelectResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-10-17 15:55:16
 */
@Repository
public class OceanBulkCargoAddressBizImpl extends BaseDao implements OceanBulkCargoAddressBiz {

    @Override
    public JsonResult list(PageResult2<T> page, OceanBulkCargoAddress oceanBulkCargoAddress, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<OceanBulkCargoAddress> rm = new BeanPropertyRowMapper<OceanBulkCargoAddress>(OceanBulkCargoAddress.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.fba");
        sql.append(",o.contacts,o.company,o.phone,o.addr1");
        sql.append(",o.city,o.province,o.country,o.postal");
        sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_ocean_bulk_cargo_address o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=o.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(oceanBulkCargoAddress)) {
            if (Utils.isNotEmpty(oceanBulkCargoAddress.getFba())) {
                sql.append(" and instr(o.fba,:fba)>0 ");
                map.put("fba", oceanBulkCargoAddress.getFba());
            }
            if (Utils.isNotEmpty(oceanBulkCargoAddress.getSort()) && Utils.isNotEmpty(oceanBulkCargoAddress.getOrder())) {
                sql.append(" order by o." + oceanBulkCargoAddress.getSort() + " " + oceanBulkCargoAddress.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<OceanBulkCargoAddress> list(OceanBulkCargoAddress oceanBulkCargoAddress, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<OceanBulkCargoAddress> rm = new BeanPropertyRowMapper<OceanBulkCargoAddress>(OceanBulkCargoAddress.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.fba");
        sql.append(",o.contacts,o.company,o.phone,o.addr1");
        sql.append(",o.city,o.province,o.country,o.postal");
        sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo_address o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(oceanBulkCargoAddress)) {
            if (Utils.isNotEmpty(oceanBulkCargoAddress.getFba())) {
                sql.append(" and instr(o.fba,:fba)>0 ");
                map.put("fba", oceanBulkCargoAddress.getFba());
            }
            if (Utils.isNotEmpty(oceanBulkCargoAddress.getSort()) && Utils.isNotEmpty(oceanBulkCargoAddress.getOrder())) {
                sql.append(" order by o." + oceanBulkCargoAddress.getSort() + " " + oceanBulkCargoAddress.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<OceanBulkCargoAddressResult> listSel(OceanBulkCargoAddress oceanBulkCargoAddress, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<OceanBulkCargoAddressResult> rm = new BeanPropertyRowMapper<OceanBulkCargoAddressResult>(OceanBulkCargoAddressResult.class);
        StringBuffer sql = new StringBuffer("select o.id value, o.fba label");
        sql.append(",o.contacts,o.company,o.phone,o.addr1");
        sql.append(",o.city,o.province,o.country,o.postal");
        sql.append(" from chf_ocean_bulk_cargo_address o ");
        sql.append(" where o.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(oceanBulkCargoAddress)) {
            if (Utils.isNotEmpty(oceanBulkCargoAddress.getFba())) {
                sql.append(" and (instr(o.fba,:fba)>0 or instr(o.contacts, :fba)>0  " +
                        "or instr(o.company, :fba)>0 or instr(o.phone, :fba)>0 " +
                        "or instr(o.addr1, :fba)>0 or instr(o.city, :fba)>0 " +
                        "or instr(o.province, :fba)>0 or instr(o.country, :fba)>0 " +
                        "or instr(o.postal, :fba)>0) " +
                        "");
                map.put("fba", oceanBulkCargoAddress.getFba());
            }
        }

        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public OceanBulkCargoAddress getById(String id) {
        RowMapper<OceanBulkCargoAddress> rm = new BeanPropertyRowMapper<OceanBulkCargoAddress>(OceanBulkCargoAddress.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.fba");
        sql.append(",o.contacts,o.company,o.phone,o.addr1");
        sql.append(",o.city,o.province,o.country,o.postal");
        sql.append(",o.create_time,o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_ocean_bulk_cargo_address o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id and sogzn.deleted=0 ");
        sql.append(" where o.deleted=0 and o.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(OceanBulkCargoAddress oceanBulkCargoAddress, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_ocean_bulk_cargo_address (");
        sql.append("organization_id,fba,contacts,company");
        sql.append(",phone,addr1,city,province");
        sql.append(",country,postal,create_user,create_time) values ( ");
        sql.append(":organization_id,:fba,:contacts,:company");
        sql.append(",:phone,:addr1,:city,:province");
        sql.append(",:country,:postal,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("fba", oceanBulkCargoAddress.getFba());
        map.put("contacts", oceanBulkCargoAddress.getContacts());
        map.put("company", oceanBulkCargoAddress.getCompany());
        map.put("phone", oceanBulkCargoAddress.getPhone());
        map.put("addr1", oceanBulkCargoAddress.getAddr1());
        map.put("city", oceanBulkCargoAddress.getCity());
        map.put("province", oceanBulkCargoAddress.getProvince());
        map.put("country", oceanBulkCargoAddress.getCountry());
        map.put("postal", oceanBulkCargoAddress.getPostal());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(OceanBulkCargoAddress oceanBulkCargoAddress, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_ocean_bulk_cargo_address set ");
        sql.append("fba=:fba,contacts=:contacts,company=:company ");
        sql.append(",phone=:phone,addr1=:addr1,city=:city ");
        sql.append(",province=:province,country=:country,postal=:postal ");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("fba", oceanBulkCargoAddress.getFba());
        map.put("contacts", oceanBulkCargoAddress.getContacts());
        map.put("company", oceanBulkCargoAddress.getCompany());
        map.put("phone", oceanBulkCargoAddress.getPhone());
        map.put("addr1", oceanBulkCargoAddress.getAddr1());
        map.put("city", oceanBulkCargoAddress.getCity());
        map.put("province", oceanBulkCargoAddress.getProvince());
        map.put("country", oceanBulkCargoAddress.getCountry());
        map.put("postal", oceanBulkCargoAddress.getPostal());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", oceanBulkCargoAddress.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_ocean_bulk_cargo_address set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }

}
